package controle.service;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;



public class UtilisateurService 

{
	static ConnectionBDService  conn = new ConnectionBDService (); 

	public int createClient(String []clientArray){

		int result = 0;

		if(clientArray.length != 8){

			return result;

		}

		String sql = "insert into client(id_client,nomClient,pwdClient,E-mail,adresse,codePostale,ville,pays) values(*,*,*,*,*,*,*,*)";

		PreparedStatement insertClient = conn.getPreparedStatement(sql);

		try {

			for(int i = 0; i < 8; i++){

				insertClient.setString(i+1, clientArray[i]);

			}

			result = insertClient.executeUpdate();

		} catch (SQLException e) {



			e.printStackTrace();

		}

		conn.closeConn();

		return result;

	}

	



	public int updateClient(String []clientArray){

		int result = 0;

		if(clientArray.length != 3){

			return result;

		}

		try{

		String sql = "select * from client where id_client='"+clientArray[0]+"'";

		ResultSet rs = conn.getResultSet(sql);

			if(rs.next()){

				for(int i = 1; i < 2; i++){

					rs.updateString(i+1,clientArray[i]);

				}

			}

			rs.updateRow();

		} catch (SQLException e) {



			e.printStackTrace();

		}

		result = 1;

		conn.closeConn();

		return result;

	}



	public int clientDelete(String id_client) throws SQLException{

		int result = 0;

		String sql = "delete from client where id_client='"+id_client+"'";

		Statement stmt = (Statement) conn.getConn().createStatement();

		result = stmt.executeUpdate(sql);

	    return result;

	}



	public static String[][] getClient() throws SQLException{

		String [][]detail = new String[0][8];

		String sqlCount = "select count(*) from client";

		String sql = "select * from client";

		java.sql.Statement stmt = conn.getConn().createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);

		ResultSet rs = stmt.executeQuery(sqlCount);

		int count = 0;

		if(rs.next()){

			count = rs.getInt(1);

		}

		detail = new String[count][8];

		rs = stmt.executeQuery(sql);

		int i = 0;

		while(rs.next()){

			for(int j = 0; j < 8; j++){

				detail[i][j] = rs.getString(j+1);

			}

			i++;

		}

		conn.closeConn();

		return detail;

		

	}

	public int verifierNum0or1(String str){

		int result = 0;

		if(Integer.parseInt(str)==1||Integer.parseInt(str)==0){

			result=1;

			return result;

		}

		else{

			return result;

		}

	}

	



}
